# 启动Python应用实例并进行错误诊断的脚本

# 检查Python是否可用
try {
    # 检查Python版本
    $pythonVersion = python --version 2>&1
    Write-Host "找到Python: $pythonVersion"
} catch {
    Write-Host "错误: 未找到Python。请确保Python已安装并添加到系统环境变量。"
    Read-Host -Prompt "按Enter键退出"
    exit 1
}

# 停止所有正在运行的Python进程
Write-Host "停止所有正在运行的Python进程..."
Get-Process -Name python -ErrorAction SilentlyContinue | Stop-Process -Force

# 等待进程停止
Start-Sleep -Seconds 2

# 启动多个应用实例
$ports = 8001, 8002, 8003
foreach ($port in $ports) {
    Write-Host "启动应用实例在端口 $port..."
    # 使用Start-Process在新窗口启动，以便查看错误
    Start-Process -FilePath "python" -ArgumentList "main.py", "--port", $port -WindowStyle Normal -WorkingDirectory "f:\bjhsp"
    # 等待实例启动
    Start-Sleep -Seconds 1
}

# 检查是否有Python进程运行
$pythonProcesses = Get-Process -Name python -ErrorAction SilentlyContinue
if ($pythonProcesses) {
    Write-Host "\n成功: 发现Python进程正在运行。"
} else {
    Write-Host "\n错误: 未找到运行中的Python进程。"
}

# 显示访问地址
Write-Host "\n应用实例地址:"
foreach ($port in $ports) {
    Write-Host "http://localhost:$port"
}

Read-Host -Prompt "按Enter键退出"